﻿using DDit.Component.Tools;
using DDit.Component.Tools.weixin;
using DDit.Core.Data.Entity.CoreEntity;
using DDit.Core.Data.IRepositories.ICoreRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDit.Core.Data.Repository.Repositories.CoreRepositories
{
    public class WxRepository : IWxRepository
    {
        public Tuple<int, List<FansUser>> GetBlackUserList(FansUser model)
        {
            int nTotal = 0;
            List<WxUser> lstWxUser;

            lstWxUser = WxUserManage.GetBlackUser(model.pageIndex, model.pageSize, ref nTotal);
            List<FansUser> lst = new List<FansUser>();
            foreach (var item in lstWxUser)
            {
                lst.Add(FansUser.Convert(item));
            }
            return new Tuple<int, List<FansUser>>(nTotal, lst);
        }

        public Tuple<int, List<UserGroup>> GetGroupList(UserGroup model)
        {
            var items = WxUserManage.GetGroup(true).AsQueryable();

            if (model.order != null && model.order.Count() > 0)
            {
                foreach (var item in model.order)
                {
                    var column = model.columns.ElementAt(int.Parse(item.column));
                    items = items.AsQueryable().OrderSort(column.data, item.dir);
                }
            }

            var resultItems = items.Skip(model.pageIndex).Take(model.pageSize).ToList();
            List<UserGroup> lst = new List<UserGroup>();
            foreach (var item in resultItems)
            {
                lst.Add(UserGroup.Convert(item));
            }
            return new Tuple<int, List<UserGroup>>(items.Count(), lst);
        }

        public List<UserGroup> GetGroupList()
        {
            var items = WxUserManage.GetGroup(false);
            List<UserGroup> lst = new List<UserGroup>();
            foreach (var item in items)
            {
                lst.Add(UserGroup.Convert(item));
            }
            return lst;
        }

        public Tuple<int, List<FansUser>> GetUserList(FansUser model)
        {
            int nTotal = 0;
            List<WxUser> lstWxUser;
            if (model.Groupid == 0)
                lstWxUser = WxUserManage.GetUserList(model.pageIndex, model.pageSize, ref nTotal);
            else
                lstWxUser = WxUserManage.GetTagUserList(model.Groupid, model.pageIndex, model.pageSize, ref nTotal);
            List<FansUser> lst = new List<FansUser>();
            foreach (var item in lstWxUser)
            {
                lst.Add(FansUser.Convert(item));
            }
            return new Tuple<int, List<FansUser>>(nTotal, lst);
        }

        public void SetUserGroup(List<string> ids, List<UserGroup> lstGroup, string oldGroup)
        {
            if (lstGroup == null)
                lstGroup = new List<UserGroup>();
            var rels = oldGroup.Split(',');
            List<int> lstOld = new List<int>();
            if (rels.Length > 0)
            {
                int oldItem = 0;
                foreach (var rel in rels)
                {
                    if (int.TryParse(rel, out oldItem))
                    {
                        lstOld.Add(oldItem);
                    }
                }
            }
            
            //添加没有的标签
            foreach (var newid in lstGroup)
            {
                if (!lstOld.Contains(newid.id))
                {
                    WxUserManage.SetUerTag(ids, newid.id, true);
                }
            }
            ///删除多的标签
            foreach (var newid in lstOld)
            {
                if (lstGroup.Where(p => p.id == newid).FirstOrDefault() == null)
                {
                    WxUserManage.SetUerTag(ids, newid, false);
                }
            }
        }
    }
}
